﻿using DGIS.Service.IService.ICoordService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DGIS.Service.Service.CoordService
{
    public class CoordConvertService : ICoordConvertService
    {
        public double[] LonLatToXY(double lon, double lat)
        {
            var x = lon * 20037508.34 / 180;
            var y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
            y = y * 20037508.34 / 180;

            return new double[] { x, y };
        }

        public double[] XYToLonLat(double x, double y)
        {
            var lon = x / 20037508.34 * 180;
            var lat = y / 20037508.34 * 180;
            lat = 180 / Math.PI * (2 * Math.Atan(Math.Exp(lat * Math.PI / 180)) - Math.PI / 2);
            return new double[] { lon, lat };
        }
    }
}
